const express = require('express')
const app = express()
const user = require('./src/router/user')
const session = require('express-session')

app.use(
  session({
    name: 'session',
    secret: 'limingru',
    resave: true,
    rolling: true,
    cookie: {
      maxAge: 30 * 60 * 1000
    }
  })
)

app.use(express.json())

app.use(express.static('static'))

app.use('/user', user)

// 打印错误
app.use(function (err, req, res, next) {
  console.log('第一个被调用了')
  console.error(err)
  next(err)
})

// 统一返回错误信息
app.use(function (err, req, res, next) {
  console.log('第二个被调用了')
  res.json({ code: -1, msg: '服务端出错' })
  next()
})

app.listen(3001, () => {
  console.log('服务启动, http://localhost:3001')
})
